//
//  QYSelectEmotionVC.swift
//  QingYuJSQ
//
//  Created by 李东 on 2024/4/5.
//

import UIKit
import SnapKit

extension Notification.Name {
    static let HideVCdNoti = Notification.Name("HideVCdNoti")
    static let DataDidChangedNoti = Notification.Name("DataDidChangedNoti")

}

class QYSelectEmotionVC: QYBaseVC {
    
    var selectCallBack:((String) -> Void)?
    lazy var emList = ["开心","难过", "生气", "烦恼", "无语", "疲惫", "休闲"]
    
    var selectDate: Date!
    var isEdit = false
    
    init(selectDate: Date = Date(), isEdit: Bool = false) {
        super.init(nibName: nil, bundle: nil)
        self.selectDate = selectDate
        self.isEdit = isEdit
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
      
        hideNavgaitionBar()
        navTitle = "树洞"
        showPopBtn = true
        titleLabel.textColor = .white
        popBtn.setImage(UIImage(named: "arrow_back_white"), for: .normal)
        view.addSubview(emView)
        emView.snp.makeConstraints { make in
//            make.top.equalTo(CGFloat.navHeight + 20)
            make.left.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(300)
        }
        
        view.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-50)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(closeHandle), name: .HideVCdNoti, object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    lazy var closeBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.setImage(UIImage(named: "icon_close"), for: .normal)
        temp.addTarget(self, action: #selector(closeHandle), for: .touchUpInside)
        return temp
    }()
    
    lazy var emView: QYEmotionView = {
        let temp = QYEmotionView()
        temp.selectCallBack = {[weak self] index in
            guard let self = self else {
                return
            }
            self.selectEmotionHandle(index: index)
        }
        return temp
    }()

    @objc func selectEmotionHandle(index: Int) {
        if isEdit {
            selectCallBack?(emList[index])
            dismiss(animated: true)
        }else {
            let vc = QYEmotionDetailVC(emotion: emList[index], selectDate: selectDate)
            vc.modalPresentationStyle = .fullScreen
            present(vc, animated: true)
        }

    }

    
    @objc func closeHandle() {
       dismiss(animated: true)
    }
   
}
